<?php
//include '../includes/01_head.php';
//include '../includes/02_menu.php';
include APPPATH . 'views/views_backend/includes/01_head.php';

?>
<div class="l-h-32 g-d-w  b-r-t-4 p-l-20 b-d-b-c-ddd dotum- p-r">
    <span>Backend</span>
    <span class="arrow_space"></span>
    <span>User Manage  </span>
    
    <div class="p-a t-8 r-8 l-h-16 z-i-100">
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p add-icon" onclick="menu.AddItem()" title="Add">Add</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p refresh-icon" onclick="user_grid().refresh()" title="Refresh">Refresh</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p setting-icon" onclick="gridSetting().open()" title="Setting">Setting</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p menu-icon" onclick="accessdenied()" title="Menu">Menu</span>
    </div>
</div>
<div class="jqx-hideborder jqx-hidescrollbars" id="jqxTabBackend">
    <ul>
        <li style="margin-left: 3px;">User list</li>
        <li>Privilege</li>
        <li>Permission</li>
        <li>Group</li>
    </ul>
    <div>
        <div class="jqx-hideborder jqxPanel">
            <br/>
            <?php include APPPATH . 'views/views_backend/includes/04_grid_option.php'; ?>
            <div id="jqxgrid_user_lst" class="-jqx-hideborder" style="border-bottom: none;border-left: none;border-right: none;"></div>
        </div>
    </div>
    <div>
        <div class="jqx-hideborder jqxPanel">	
            a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>a<br/>
        </div>	
    </div>
    <div>
        <div class="jqx-hideborder jqxPanel">	
            a<br/>a<br/>
        </div>	
    </div>
    <div>
        <div class="jqx-hideborder jqxPanel">	
            a<br/>a<br/>
        </div>	
    </div>
</div>
<div class="l-h-28 g-d-w  b-r-b-4 p-l-20 b-d-t-c-ddd dotum- p-r">
    <div class="l-h-16 z-i-100 p-b-8 p-t-8 dotum">
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p add-icon" onclick="menu.AddItem()" title="Add">Add</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p refresh-icon" onclick="user_grid().refresh()" title="Refresh">Refresh</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p setting-icon" onclick="gridSetting().open()" title="Setting">Setting</span>
        <span class="l-h-16 p-l-20 p-r-8 f-l hover-50 c-s-p menu-icon" onclick="accessdenied()" title="Menu">Menu</span>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
</div>
<div class="grid_12- d-p-n">
    <div id="editor_box" class="b-d-c-ccc- p-r b-g-c-w- p-4">
        <div class="grid_x biz"></div>
    </div>
</div>
<script type="text/javascript">
    var menu=(function(){
        return{
            AddItem:function(){
                _detail('');
            },
            EditItem:function(id){
                _detail(id);
            },
            Save:function(){
                update_ause();
            },
            Cancel:function(){
                $('#editor_box').dialog('close');
            },
            DeleteItem:function(id){
                accessdenied();
            },
            RestoreItem:function(){
                accessdenied();
            },
            Permission:function(id){
                _permission(id);
            },
            PrivateItem:function(){
                accessdenied();
            },
            Setting:function(){
                bckdialog({
                    message:$('#window-sOpt'),
                    icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                    title:'Setting'
                }).open();
            },
            Refresh:function(){
                $("#jqxGrid").jqxGrid('updatebounddata');
            }
        };
    }());
    function update_ause(){
        if( $('#ause_frm').validationEngine('validate') === false)return false;
        new backend({
            url         :   base_url+"backend/user/save",
            data        :   {
                ause_id        :   $("#ause_id").val(),
                ause_name      :   $("#ause_name").val(),
                ause_username  :   $("#ause_username").val(),
                ause_email     :   $("#ause_email").val(),
                ause_password  :   $("#ause_password").val(),
                ause_position  :   $("#ause_position").val(),
                ause_status    :   $("#ause_status").val()
            },
            datatype    :   "json",
            callback    :   function(data){
                if(data.result<0){
                    new bckdialog({type:'error'}).open(data.message);
                }else{
                    uiMessage(data.message);
                }
            }
        }).call();
        return false;
    }
    function randomString() {
        new backend({
            url         :   base_url+"backend/user/randomstring",
            data        :   {},
            datatype    :   "text",
            callback    :   function(data){
                $("#ause_password").val(data);
            }
        }).call();
    }
    function _detail(id){
        backendloading();
        $("#editor_box .biz").load(
        base_url+"backend/user/editor/" + id,
            function() {
                bckdialog({
                    title:'User',message:$('#editor_box')
                }).open();
//                islc({
//                    element:'#cate_parent',num:2,minwidth:'480px',height:'240px',
//                    multi:false,aclass:'_mul',style :{'margin-top':'-120px'},
//                    title:'Parent',filter:true
//                });
//                $( "#tabs" ).tabs();
                backendloaded();
                $("#ause_frm").validationEngine();
            }
        );
    }
    function _permission(id){
        backendloading();
        $("#editor_box .biz").load(
        base_url+"backend/user/detail/" + id,
            function() {
                backendloaded();
                bckdialog({
                    title:'<span class="dotum-">Change Permission</span>',
                    message:$('#editor_box')
                }).open();
            }
        );
    }
    function changeprivilege(user_id,privilege_id,value,secretkey,signature){
        //if(getCheckedRadio(id)!==value){
        new backend({
            url         :   base_url+"backend/user/setprivilege",
            data        :   {
                user_id		:	user_id,
                privilege_id    :	privilege_id,
                permission	:	value,
                secretkey	:	secretkey,
                signature	:	signature
            },
            datatype    :   "json",
            callback    :   function(data){
                try{
                    if(data.result<0){
                        bckdialog({
                            type:'error'
                        }).open(data.message);
                    }else{
                        uiMessage(data.message);
                    }
                }catch(e){
                    bckdialog({
                        type:'error'
                    }).open("Sorry. Your request could not be completed.<br/> Please check your input data and try again.");
                }
            }
        }).call();
    //}
    }
    $(document).ready(function() {
        $("#jqxTabBackend").jqxTabs({
            theme: 'metro', height: 'auto',  scrollable: true, 
            selectionTracker: true, animationType: 'fade',
            initTabContent: function(tab) {
                switch (tab) {
                    case 0:
                        user_grid().init_grid();
                        gridSetting().init();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        
                        break;
                    case 5:
                        
                        break;
                }
            }
        });
    });
    function user_grid(){
    var grid_element=$("#jqxgrid_user_lst");
        return {
            refresh:function(){ grid_element.jqxGrid('updatebounddata'); },
            init_grid:function(){
                
                var source =
                {
                    datatype: "json",type : "POST",
                    datafields: [
                        { name: '_id' },
                        { name: 'ause_id' },
                        { name: 'ause_key' },
                        { name: 'ause_name' },
                        { name: 'ause_username' },
                        { name: 'ause_email' },
                        { name: 'ause_position'},
                        { name: 'ause_status' },
                        { name: 'ause_insert'},
                        { name: 'ause_update'},
                        { name: 'ause_authority'}
                    ],
                    id: 'Id',
                    url: base_url+'backend/user/binding.html',
                    filter      : function(){ grid_element.jqxGrid('updatebounddata', 'filter'); },
                    sort        : function(){ grid_element.jqxGrid('updatebounddata'); },
                    root        : 'rows',
                    beforeprocessing: function(data){ source.totalrecords = data.total_rows; }
                };
                var dataAdapter = new $.jqx.dataAdapter(source);
                grid_element.jqxGrid({
                    rendergridrows: function(obj){
                        return obj.data;    
                    },
                    width: '100%',//height:'100%',
                    source: dataAdapter,
                    theme: theme,sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    showemptyrow: false,
                    virtualmode:true,
                    columns: [
                        { 
                            text: '', dataField: '_id',width:80,filterable:false,editable:false,sortable:false,
                            cellsrenderer : function (row, column, value) {
                                var str="<span style='margin: 4px; float: left;'>";
                                if(value && value>0){
                                    try{
                                        //value = $.parseJSON(value);
                                        str+="\
                                            <div onclick=\"menu.EditItem('"+value+"');\" \
                                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 edit-icon' title='Edit'></div>\
                                        ";
                                        str+="\
                                            <div onclick=\"menu.DeleteItem('"+value+"');\" \
                                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 del-icon' title='Delete'></div>\
                                        ";
                                        str+="\
                                            <div onclick=\"menu.Permission('"+value+"');\" \
                                            class='w-16 h-16 f-l m-r-4 c-s-p hover-50 user-icon' title='Permission'></div>\
                                        ";
                                    }catch(e){}
                                }
                                str+="</span>";
                                return str;
                            }

                        },
                        { text: 'Id', dataField: 'ause_id',width:60,filterable:false },
                        { text: 'Key', dataField: 'ause_key',width:100,hidden:true},
                        { text: 'Name', dataField: 'ause_name' },
                        { text: 'UserName', dataField: 'ause_username' },
                        { text: 'Email', dataField: 'ause_email' },
                        { text: 'Position', dataField: 'ause_position', width: 100,columntype:"numberinput",filtertype:"number",hidden:true},
                        { 
                            text: 'Status', dataField: 'ause_status',width:60,
                            type:"bool",columntype:"checkbox",threestatecheckbox:false,filtertype:"bool"
                        },
                        { text: 'ause_insert', dataField: 'ause_insert',width:120,cellsformat:'yyyy-MM-dd HH:mm:ss'},
                        { text: 'ause_update', dataField: 'ause_update',width:120,cellsformat:'yyyy-MM-dd HH:mm:ss',hidden:true}
                    ]
                });
            }
        };
    };
    function gridSetting(){
        return {
            init:function(){
                $("#jqxListbox_columns").jqxListBox({ 
                    checkboxes: true,
                    source: [
                        {"label":"ID","value":"ause_id","checked":true},
                        {"label":"Name","value":"ause_name","checked":true},
                        {"label":"Username","value":"ause_username","checked":false},
                        {"label":"Email","value":"ause_email","checked":true},
                        {"label":"Group","value":"agrp_name","checked":true},
                        {"label":"Active","value":"ause_status","checked":true},
                        {"label":"Insert","value":"ause_insert","checked":true},
                        {"label":"Update","value":"ause_update","checked":false},
                        {"label":"Delete","value":"ause_delete","checked":false}
                    ],
                    width: 234, 
                    height: 184, 
                    theme: "classic" 
                }).bind('checkChange', function (event) {
                    try{
                        if (event.args) {
                            var hide=1;
                            if (event.args.checked) {
                                $('#jqxgrid_user_lst').jqxGrid('showcolumn', event.args.value);  
                                hide=0;
                            }else {
                                $('#jqxgrid_user_lst').jqxGrid('hidecolumn', event.args.value);
                            }
                        }
                    }catch(e){
                        uiMessage(e.message);
                    }
                });
            },
            open:function(){
                bckdialog({
                    message:$('#window-sOpt'),
                    icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                    title:'Setting'
                }).open();
            }
        };
    }
</script>
<?php include APPPATH . 'views/views_backend/includes/03_foot.php'; ?>